package com.tencent.controller;

import com.tencent.entity.Diet;
import com.tencent.entity.Member;
import com.tencent.entity.Result;
import com.tencent.service.DietService;
import com.tencent.service.LoginService;
import com.tencent.service.MemberYService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Fame
 * @date: 2022-08-30 15:28
 * @description:
 */
@RestController
@RequestMapping("/diet")
public class DietController {

    @Autowired
    private DietService dietService;

    @Autowired
    private MemberYService memberYService;

    @Autowired
    private LoginService loginService;

    @GetMapping("/findAll")
    public Result findAll(){
        return Result.success(dietService.getAll());
    }

    @GetMapping("/byLevel")
    public Result finaByLevel(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = (String) authentication.getPrincipal();
        Integer id = loginService.findIdByUsername(username);
        Member member = memberYService.getById(id);
        return Result.success(dietService.getByLevel(member.getLevel()));
    }
}
